<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计数组中元素出现的次数</title>
</head>
<!-- [1,2,3,1,1,1,2,2,3,3,4]
1出现的次数 4
2出现的次数 3
3出现的次数 3
4出现的次数 1
 -->
<body>
    <script>
        var arr=[1,2,3,1,1,1,2,2,3,3,4];
        var res=arr.reduce(function(pre,cur){
            // pre={}
            // if(pre[cur]){
            //     pre[cur]+=1;
            // }else{
            //     pre[cur]=1;
            // }
            if(cur in pre){
                pre[cur]+=1
            }else{
                pre[cur]=1
            }
            return pre
        },{})
        for(var key in res){
            console.log(key+'出现次数'+res[key]);
        }
    </script>
    
</body>
</html>